var express = require('express');
var router = express.Router();
var formidable = require('formidable');
var fs = require('fs'),
    TITLE = '图片上传',
    domain = "http://localhost:8089/";//上传地址
    AVATAR_UPLOAD_FOLDER = '/image/';//上传文件夹
var app=express();
const { UploadFiles }= require('../common/upload');
app.all('*', function (req, res, next) {
    res.header('Access-Control-Allow-Origin', req.headers.origin || '*');
    res.header('Access-Control-Allow-Headers', 'Content-Type,Content-Length, Authorization,Origin,Accept,X-Requested-With');
    res.header('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
    res.header('Access-Control-Allow-Credentials', true);
    res.header('X-Powered-By', ' 3.2.1');
    res.header('Content-Type', 'application/json;charset=utf-8');
    if (req.method === 'OPTIONS') {
        res.sendStatus(200);
    } else {
        next();
    }
  });
router.all('/',function(req,res){
    res.send({
        msg:'欢迎上传图片'
    })
})
//上传图片
router.post('/addimg', async function (req, res) {
    const upload=new UploadFiles(req);
    const rest=await upload.imgSave();
    console.log('rest:',rest)
    res.send({
        msg:'上传图片成功',
        status:4,
        data:rest
    })
});
module.exports = router;